﻿using System;
using System.Drawing;

public interface IShape
{
    double CalculateArea();
    bool IsValid();
}

public class Rectangle : IShape
{
    public double Width { get; set; }
    public double Height { get; set; }

    public Rectangle(double width, double height)
    {
        Width = width;
        Height = height;
    }

    public double CalculateArea()
    {
        if (IsValid())
        {
            return Width * Height;
        }
        else
        {
            Console.WriteLine($"长方形不合法，宽度为 {Width}，高度为 {Height}。");
            return 0;
        }
          
    }

    public bool IsValid()
    {
        return Width > 0 && Height > 0;
    }
}

public class Square : IShape
{
    public double Side { get; set; }

    public Square(double side)
    {
        Side = side;
    }

    public double CalculateArea()
    {
        if (IsValid())
        {
            return Side * Side;
        }
        else
        {
            Console.WriteLine($"正方形不合法,边长为{Side}。");
            return 0;
        }
    }

    public bool IsValid()
    {
        return Side > 0;
    }
}

public class Triangle : IShape
{
    public double A { get; set; }
    public double B { get; set; }
    public double C { get; set; }

    public Triangle(double a, double b, double c)
    {
        A = a;
        B = b;
        C = c;
    }

    public double CalculateArea()
    {
        if (IsValid())
        {
            double s = (A + B + C) / 2;
            return Math.Sqrt(s * (s - A) * (s - B) * (s - C));
        }
        else
        {
            Console.WriteLine($"三角形不合法,边长为{A},{B},{C}。");
            return 0;
        }
    }

    public bool IsValid()
    {
        // Check if the sides form a valid triangle using the triangle inequality theorem
        return A + B > C && A + C > B && B + C > A;
    }
}


public class Factory
{
    public IShape CreateShape(string type, double[] dimensions)
    {
        switch (type)
        {
            case "Rectangle":
                if(dimensions.Length >= 2)
                {
                    Console.WriteLine("已用前两条边创建三角形");
                    return new Rectangle(dimensions[0], dimensions[1]);
                }
                else
                {
                    throw new InvalidOperationException("边数不够喵");
                }
                
            case "Square":
                if (dimensions.Length >= 1)
                {
                    Console.WriteLine("已用第一条边创建正方形");
                    return new Square(dimensions[0]);
                }
                else
                {
                    throw new InvalidOperationException("边数不够喵");
                };

            case "Triangle":
                if (dimensions.Length >= 3)
                {
                    Console.WriteLine("已用前三条边创建三角形");
                    return new Triangle(dimensions[0], dimensions[1], dimensions[2]);
                }
                else
                {
                    throw new InvalidOperationException("边数不够喵");
                };


            default:
                throw new InvalidOperationException("没有这个形状喵");
        }
    }
}


class ran
{
    public string Rands(string[]a)
    {
        Random rnd = new Random();
        int len = a.Length;
        int i=rnd.Next(0,len-1);
        return a[i];
    }

    public double[] RandomDoubleArray(int size)
    {
        double[] array = new double[size];
        Random random = new Random();

        for (int i = 0; i < size; i++)
        {
            array[i] = 1 + (random.NextDouble() * (10.0 - 1));

        }

        return array;
    }
}




public class Program
{
    public static void Main(string[] args)
    {
        Factory factory = new Factory();

        string[] names = { "Square", "Triangle", "Rectangle" };

        double res = 0.0;
        int novalid = 0;

        ran ran = new ran();
        for(int i=0;i<10;i++)
        {
            string name = ran.Rands(names);
            double[]d=ran.RandomDoubleArray(3);
            IShape t_shape = factory.CreateShape(name, d);
            double area = t_shape.CalculateArea();
            if (area==0)
                novalid++;
            res += area;
        }
        Console.WriteLine($"总面积为{res},不合法的图形有{novalid}个喵");
    }
}

